package arg

import (
	"errors"
	"fmt"
	"github.com/gurkankaymak/hocon"
	"db2s/global"
	"os"
)

type TaskS struct {
	Object         string   `json:"object"`
	Limiter        string   `json:"limiter"`
	IgCount        bool     `json:"igCount"`
	ActiveMode     []string `json:"active"`
	SqlMode        bool     `json:"sqlMode"`
	LowerCase      bool     `json:"case"`
	IgColumn       bool     `json:"igColumn"`
	TimeOut        int      `json:"timeOut"`
	HistogramScale int      `json:"histogramScale"`
	Scn            string   `json:"scn"`
	Breakpoint     bool     `json:"breakpoint"`
	BreakPointFin  *os.File
}

func getTaskRulesDefVal() TaskS {
	return TaskS{
		Object:     "",
		Limiter:    ",",
		SqlMode:    false,
		IgCount:    false,
		ActiveMode: []string{"sync"},
		LowerCase:  true,
		IgColumn:   false,
	}
}

func fileEmpty(filename string) (bool, error) {
	file, err := os.Stat(filename)
	if err != nil {
		return false, err
	}
	if file.Size() == 0 {
		return false, nil
	}
	return true, nil
}
func taskMustParameterCheck(Task TaskS) (err error) {
	var (
		b bool
	)
	if len(Task.Object) == 0 {
		err = errors.New(fmt.Sprintf("object parameter set empty"))
		return
	}
	if !global.FileExists(Task.Object) {
		err = errors.New(fmt.Sprintf("object set value {%v} not exist!!!", Task.Object))
		return
	}
	if b, err = fileEmpty(Task.Object); err != nil {
		return err
	}
	if !b {
		err = errors.New(fmt.Sprintf("%v object file size is empty", Task.Object))
		return
	}
	//var modeExist bool
	//for _, v := range getRulesCheck().FunctionalMode {
	//	if strings.EqualFold(v, Task.ActiveMode) {
	//		modeExist = true
	//	}
	//}
	//if !modeExist {
	//	err = errors.New(fmt.Sprintf("active set value error. Execution process{set value:%v,expected value:%v}", Task.ActiveMode, strings.Join(getRulesCheck().FunctionalMode, ",")))
	//	return
	//}
	return
}

// tasksSecondaryLevelParameter
func tasksParameter(hocon *hocon.Config) (Task TaskS, err error) {
	//必要参数检查
	if Task, err = taskRuleArgParameter(hocon); err != nil {
		return TaskS{}, err
	}
	if err = taskMustParameterCheck(Task); err != nil {
		return
	}
	return
}
